package com.cn.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.cn.domain.Cart;
import com.cn.domain.Product;
import com.cn.domain.User;
import com.cn.service.CartService;
import com.cn.service.impl.CartServiceImpl;

@WebServlet("/PreCartServlet")
public class PreCartServlet extends HttpServlet {
	private CartService cartService=new CartServiceImpl();
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		User user=(User) req.getSession().getAttribute("user");
		//然后到购物车里面
		Map<String,String[]> map=req.getParameterMap();
		List<Cart> cartList=null;
		Cart cart=new Cart();
		Product product=new Product();
		try {
			//封装成了一个user对象
			BeanUtils.populate(cart, map);
			cart.setUserid(user.getHu_user_id());
			
			BeanUtils.populate(product, map);
			
			//添加到购物车
			cartService.addCart(cart);
			
			//然后更新我们的这个库存
			//cartService.updateCartById(num, id);
			
			
			//按照USERID和PID查询所有的cart物品
			cartList=cartService.queryAllCart(user.getHu_user_id());
			System.out.println("cartList="+cartList);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		req.getSession().setAttribute("shoppingList", cartList);
		req.getRequestDispatcher("/GoToOrderJspServlet").forward(req, resp);
	}
}
